package com.yiren.dbaa.net;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Message;

import com.yiren.dbaa.constant.NetworkOperCode;
import com.yiren.dbaa.global.GlobalApplication;
import com.yiren.dbaa.view.dialog.CustomDialog;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import org.json.JSONObject;

/**
 * 统一处理网络结果集
 *
 * @author Administrator
 */
public class NetworkProcessing {

    private static CustomDialog.Builder builder;

    /**
     * 统一处理Message
     */
    public static void baseHandleMessage(Context context, Message msg) {
        LoadDialog.dismiss(context);
        try {
            switch (msg.arg1) {
                case NetworkOperCode.ERROR_CODE_400:
                    try {
                        JSONObject obj = new JSONObject(msg.obj.toString());
                        String errorStr = obj.getString("error");
                        if (null != errorStr && !"".equals(errorStr)) {
                            if ("invalid_grant".equals(errorStr)) {
                                MessageBox.show(context, obj.getString("error_description"));
                            } else {
                                MessageBox.show(context, obj.getString("message"));
                            }
                        } else {
                            MessageBox.show(context, msg.obj.toString());
                        }
                    } catch (Exception e) {
                        MessageBox.show(context, "服务器连接失败...");
                        e.printStackTrace();
                    }
                    break;
                case NetworkOperCode.ERROR_CODE_401:
                    NetworkProcessing.afreshLogin((Activity) context);
                    break;
                case NetworkOperCode.ERROR_CODE_403:
                    MessageBox.show(context, "没有访问权限(403)");
                    break;
                case NetworkOperCode.ERROR_CODE_404:
                    MessageBox.show(context, "访问路径不存在(404)");
                    break;
                case NetworkOperCode.ERROR_CODE_408:
                    MessageBox.show(context, "服务器等候请求时发生超时");
                    break;
                case NetworkOperCode.ERROR_CODE_500:
                    MessageBox.show(context, "系统内部错误(500)");
                    break;
                case NetworkOperCode.ERROR_CODE_502:
                    MessageBox.show(context, "错误网关");
                    break;
                case NetworkOperCode.ERROR_CODE_504:
                    MessageBox.show(context, "网关超时");
                    break;
                case NetworkOperCode.REQUEST_SERVER_ERROR:
//                    showCheckAvailable2(context);
                    break;
                case NetworkOperCode.OSS_ERROR:
//                    MessageBox.show(context, "OSS错误");
                    break;
                case NetworkOperCode.SYSTEM_TIME:
                    CustomDialog.Builder builder = new CustomDialog.Builder(context);
                    builder.setMessage("请检查本机时间是否为最新时间");
                    builder.setTitle("温馨提示");
                    builder.setCanceled(false);
                    builder.setPositiveButton("关闭", (dialog, which) -> {
                        dialog.dismiss();
//                        SysApplication.getInstance().exit();
                    });
                    builder.create().show();
                    break;
                default:
                    if (msg.arg1 != NetworkOperCode.REQUEST_SERVER) {
                        MessageBox.show(context, "错误: " + msg.arg1 + "\n" + msg.obj);
                    }
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 会话超时，要求重新登录
     *
     * @param activity
     */
    public static void afreshLogin(Activity activity) {
        if (!activity.isFinishing() && null == builder) {
            builder = new CustomDialog.Builder(activity);
            // builder.setMessage("会话已失效，请重新登录");
            builder.setMessage("登录已过期，请重新登录\n您是否现在登录？");
            builder.setTitle("温馨提示");
            builder.setCanceled(false);
            builder.setPositiveButton("马上登录", (dialog, which) -> {
                GlobalApplication.getInstance().logOut(activity);
                dialog.dismiss();
            });
            builder.setNegativeButton("暂不登录", (dialog, which) -> dialog.dismiss());
            if (!activity.isFinishing()) {
                builder.create().show();
            }
        }
    }

}
